package com.itsccn.quartzdemo.scheduler;

import java.util.Date;

import org.apache.log4j.Logger;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.itsccn.quartzdemo.Job.HelloJob;

public class SimpleExample {
	public void run() throws Exception {
		Logger log = Logger.getLogger(getClass());
		log.info("------- Initializing ----------------------");
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();

		log.info("------- Initialization Complete -----------");

		Date startTime = DateBuilder.nextGivenSecondDate(null, 15);

		log.info("------- Scheduling Job  -------------------");

		JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group").build();
		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startAt(startTime).build();
		sched.scheduleJob(job, trigger);

		log.info(job.getKey() + " will run at: " + startTime);
		sched.start();

		log.info("------- Started Scheduler -----------------");
		log.info("------- Waiting 65 seconds... -------------");
		try {
			Thread.sleep(65L * 1000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		log.info("------- Shutting Down ---------------------");
		sched.shutdown(true);
		log.info("------- Shutdown Complete -----------------");
	}
}
